
var NEVERLOST_SAVE_INTERVAL = 6000;

var neverlostLogger = {

	log: function(msg) {
		var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
		aConsoleService.logStringMessage("Neverlost " + msg);
	},

	error: function(msg) {
		neverlostLogger.log("error: " + msg);
	},

	warning: function(msg) {
		neverlostLogger.log("warning: " + msg);
	},

	info: function(msg) {
		neverlostLogger.log("info: " + msg);
	},

	// debug message -- comment the body for production release
	debug: function(msg) {
		neverlostLogger.log("debug: " + msg);
	}
}

var neverlostHandler = {

	mScheduledDocs: new Array(),

	mTimeoutId: undefined,

	// fires when main browser window is opened
	onPageLoadStart: function() {
		// the browser (appcontent)
		var appcontent = document.getElementById("appcontent");
		if (appcontent)
			appcontent.addEventListener("DOMContentLoaded", neverlostHandler.onPageLoadEnd, true);
	},

	// fires when new content is loaded to any browser window
	onPageLoadEnd: function(aEvent) {
		var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
		if (! doc) {
			neverlostLogger.error("The document generating the DOMContentLoaded is undefined");
			return;
		}

		// if the document contains one or more forms register keypres and submit events
		if (doc.getElementsByTagName("form").length > 0) {
			// each keypress sets the "dirty" flag on the document
			doc.addEventListener("keypress", neverlostHandler.onDocumentKeypress, false);
			// submit means one more aditional autosave if dirty
			doc.addEventListener("submit", neverlostHandler.saveScheduled, false);
		}
	},

	// fires when keypress occur
	onDocumentKeypress: function(aEvent) {
		var element = aEvent.originalTarget;
		if (element) {
			neverlostHandler.scheduleForSave(element.ownerDocument);
		}
	},

	// schedules documents form for save in the next interval
	scheduleForSave: function(doc) {
		if (! doc) {
			neverlostLogger.warning("Nothing to schedule");
			return;
		}

		// if this is first document to save we start watches
		if (neverlostHandler.mScheduledDocs.length == 0 && doc.defaultView) {
			neverlostHandler.mTimeoutId = doc.defaultView.setTimeout(neverlostHandler.saveScheduled, NEVERLOST_SAVE_INTERVAL);
			neverlostLogger.debug("Next autosave is scheduled in " + NEVERLOST_SAVE_INTERVAL + " miliseconds");
		}

		// append to the schedule queue
		neverlostLogger.debug("Adding document '" + doc.title + "' in the autosave queue (" + neverlostHandler.mScheduledDocs.length + ")");
		neverlostHandler.mScheduledDocs.push(doc);
	},

	// delegates the save
	saveScheduled: function() {
		if (!neverlostHandler.mScheduledDocs || neverlostHandler.mScheduledDocs.length == 0) {
			neverlostLogger.warning("Nothing to auto save, skipping");
		}

		//alert(neverlostHandler.mScheduledDocs[0]);
		// clear the timer first (if not yet stopped)
		if (neverlostHandler.mScheduledDocs[0].defaultView) {
			neverlostHandler.mScheduledDocs[0].defaultView.clearTimeout(neverlostHandler.mTimeoutId);
			neverlostHandler.mTimeoutId = undefined;
		}

		// proceed
		neverlostStore.saveForms(neverlostHandler.mScheduledDocs);

		// empty the queue
		neverlostHandler.mScheduledDocs = new Array();

		// always return true -- it is hooked on form submit
		return true;
	}

}

var neverlostStore = {

	saveForms: function(doc) {
	}

}

// register broswer (main) page load handler
window.addEventListener("load", neverlostHandler.onPageLoadStart, false);

// EOF, hope you neverlost

